

function PowerNet(options) {
	this.options  = options
	var graph = new Q.Graph(this.options.target)
	if(options.editable) {
		graph.callLater(function(){graph.editable = true});
	}
	this.graph = graph
	// $($(".Q-Canvas")[1]).hide(); 
	this.elements = {} //id:element
}

PowerNet.prototype.setGraph = function(props) {
	for(var k in props) {
		this.graph[k] = props[k]
	}
}
PowerNet.prototype.createElement = function(element) {
	var e;
	var props = element.Props
	if(element.Type==1) { //node
		e = this.graph.createNode(props.name , props.x ,props.y)
	}
	if(element.Type==2) { //edge
		e = this.graph.createEdge(props.name , this.elements[props.from] ,this.elements[props.to])
	}
	if(element.Type==3) { //text
		e = this.graph.createText(props.name, props.x ,props.y)
	}
	if(element.Type==4) { //group
		e = this.graph.createGroup(props.name)
		if('x' in props) {
			e.location = new Q.Point(props.x ,props.y)
		}
	}
	if(element.Type==5) { //subnetwork
		e = this.graph.createNode(props.name , props.x ,props.y)
		e.enableSubNetwork = true;
	}
	//设置属性
	var skipProps = {'name':true,'x':true,'y':true,'from':true,'to':true,'parent':true}
	this.setProps(e, props , skipProps)

	//设置parent
	if(props.parent) {
		parent = this.elements[props.parent]
		if(parent) {
			e.parent = parent
			// parent.addChild(e)
		}
	}


	e.set('element' , element)
	e.set('id',element.Id)
	this.elements[element.Id] = e
}

PowerNet.prototype.getElement = function(id) {
	return this.elements[id]
}
PowerNet.prototype.setProps = function(ele,props,skipProps) {
	if(!ele){
		return
	}
	for(var k in props) {
		if(skipProps && skipProps[k]) {
			continue
		}
		if(k =='styles') {
			var styles = props[k]
			for(var s in styles) {
				ele.setStyle(s,styles[s])
			}
			continue
		}
		ele[k] = props[k]
	}
}
PowerNet.prototype.clear = function() {
	this.graph.clear()
	this.elements = {}
}
PowerNet.prototype.removeElement = function(id) {
	var e = this.elements[id]
	if(e) {
		delete this.elements[id]
		this.graph.removeElement(e)
	}
}
PowerNet.prototype.loadElements = function(elements) {
	for(var i=0;i<elements.length;i++) {
		var element = elements[i]
		if(element.Type==4) { //create group first
			this.createElement(element)
		}
	}
	for(var i=0;i<elements.length;i++) {
		var element = elements[i]
		if(element.Type==5) { //create subnetwork parent first
			this.createElement(element)
		}
	}
	for(var i=0;i<elements.length;i++) {
		var element = elements[i]
		if(element.Type==1||element.Type==3) { //create group node and text
			this.createElement(element)
		}
	}
	for(var i=0;i<elements.length;i++) {
		var element = elements[i]
		if(element.Type==2) { //create edge last
			this.createElement(element)
		}
	}
}


PowerNet.prototype.setMovable = function(b) {
	this.graph.forEach(function(e) {
		e.movable=b
	})
}